<script setup lang="ts">
import { routes } from "../router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import ACCESS_ENUM from "@/access/accessEnum";

const router = useRouter();
const store = useStore();

const visibleRoutes = computed(() => {
  return routes.filter((item) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    if (
      !checkAccess(store.state.userStore.loginUser, item.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

//刷新时的默认路由
const selectedKey = ref(["/"]);
router.beforeEach((to, from, next) => {
  selectedKey.value = [to.path];
  next();
});

setTimeout(() => {
  store.dispatch("userStore/getLoginUser", {
    userName: "YXM",
    role: ACCESS_ENUM.ADMIN,
  });
}, 3000);
</script>

<template>
  <a-row class="grid-demo" style="margin-bottom: 16px" align="center">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKey"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item key="0" disabled :style="{ marginRight: '32px' }">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" />
            <div class="title">OJ</div>
          </div>
        </a-menu-item>
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div>{{ store.state?.userStore?.loginUser?.userName ?? "未登录" }}</div>
    </a-col>
  </a-row>
</template>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.logo {
  width: 48px;
  height: 48px;
}

.title {
  color: #000000;
  margin-left: 16px;
}
</style>
